package com.feeyo.groundservice.composewidget.util

import android.text.Spanned
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.webkit.WebView
import android.widget.TextView
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.feeyo.groundservice.composewidget.R
import com.feeyo.groundservice.composewidget.view.util.VerticalSpace

/**
 *作者：QLH on 2024-08-26
 *描述：
 */

//横线
@Composable
fun HorizontalLine(color: Color, height: Dp = 1.dp) {
    Canvas(modifier = Modifier.fillMaxWidth().height(height), onDraw = {
        drawLine(color = color, Offset(0f, 0f), Offset(size.width, 0f), 2f)
    })
}

//虚线
@Composable
fun DashLine(
    modifier: Modifier = Modifier,
    color: Color = Color.Black,
    height: Dp = 1.dp,
    dashWidth: Float = 1f,
    intervalWidth: Float = 1f,
) {
    val strokeWidth = height.dpToPx()
    Canvas(modifier = Modifier.height(height).then(modifier), onDraw = {
        drawLine(
            color = color,
            Offset(0f, 0f),
            Offset(size.width, 0f),
            strokeWidth,
            pathEffect = PathEffect.dashPathEffect(
                floatArrayOf(dashWidth, intervalWidth), 0f
            )
        )
    })
}

@Composable
fun ArrowImage(size: Dp, hint: Color) {
    Image(
        painter = painterResource(R.drawable.icon_arrow_right),
        contentDescription = null,
        contentScale = ContentScale.Inside,
        modifier = Modifier.size(size),
        colorFilter = ColorFilter.tint(hint)
    )
}

/**
 * 绘制矩形虚线
 * @param color 颜色
 * @param height 粗细
 * @param cornerRadius 圆角
 * @param dashWidth 虚线宽度
 * @param intervalWidth 虚线间隔宽度
 * **/

fun Modifier.drawRectDash(
    color: Color = Color.Black,
    height: Float = 1f,
    cornerRadius: Float = 1f,
    dashWidth: Float = 1f,
    intervalWidth: Float = 1f,
): Modifier {
    return this.drawBehind {
        drawRoundRect(
            color = color,
            cornerRadius = CornerRadius(cornerRadius),
            style = Stroke(
                width = height,
                pathEffect = PathEffect.dashPathEffect(
                    floatArrayOf(dashWidth, intervalWidth), 0f
                )
            )
        )
    }
}

@Composable
fun HtmlWebView(
    msg: String,
    modifier: Modifier,
) {
    AndroidView(
        modifier = modifier,
        factory = {
            WebView(it).apply {
                loadData(msg, "text/html", "utf-8")
            }
        }
    )
}

@Composable
fun HtmlText(
    msg: Spanned,
    modifier: Modifier = Modifier,
    txtColor: Int,//color
    fontSize: Float,//px
    maxLines: Int = 3
) {
    AndroidView(
        factory = {
            TextView(it)
        },
        update = {
            it.apply {
                text = msg
                this.maxLines = maxLines
                setTextColor(txtColor)
                setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
                setLineSpacing(1f, 1f)
                ellipsize = TextUtils.TruncateAt.END
            }
        }
    )
}


@Composable
fun CaptureComposeView(
    content: @Composable () -> Unit,
    localView: (View) -> Unit
) {
    AndroidView(
        factory = {
            ComposeView(it)
        },
        update = {
            it.apply {
                setContent {
                    content()
                }
                localView(this)
            }
        }
    )
}

@Composable
fun EmptyListView(
    placeholder: Int = R.drawable.icon_empty_list,
    tip: String = "暂无数据"
) {
    Column(
        modifier = Modifier.fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Image(
            painter = placeholder.drawableToPainter(),
            contentDescription = tip,
            contentScale = ContentScale.Inside
        )
        VerticalSpace(R.dimen.d5.dimenToDp())
        Text(
            text = tip,
            fontSize = R.dimen.d12.dimenToSp(),
            color = R.color.black_66.toComposeColor()
        )
    }
}